<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >用户管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>个人用户</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="individual_user_content">
        <!-- 搜索框 -->
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <div class="individual_user_search">
              <el-input
                v-model="searchKey"
                placeholder="请输入"
                size="small"
              ></el-input>
              <el-button
                type="primary"
                size="small"
                style="margin-left: 5px"
                @click="searchFn()"
                >搜索</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <div v-if="userListData != undefined && userListData.length > 0">
          <el-row>
            <el-col :span="24">
              <el-table
                :data="userListData"
                border
                size="mini"
                style="width: 100%"
                :cell-style="cellStyleFn"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>

                <el-table-column
                  prop="name"
                  label="微信名称"
                  align="center"
                  min-width="200"
                ></el-table-column>

                <el-table-column label="微信头像" width="80" align="center">
                  <template slot-scope="scope">
                    <div class="user_image_box">
                      <img class="user_image" :src="scope.row.avatarurl" />
                    </div>
                  </template>
                </el-table-column>

                <el-table-column
                  prop="phone"
                  label="手机号码"
                  align="center"
                  min-width="200"
                ></el-table-column>

                <el-table-column
                  prop="created_at"
                  label="注册时间"
                  min-width="200"
                  align="center"
                ></el-table-column>

                <el-table-column label="操作" min-width="200" align="center">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="info"
                      @click="blockFn(scope.$index, scope.row)"
                      v-if="scope.row.status == 0"
                      >冻结</el-button
                    >
                    <el-button
                      size="mini"
                      type="danger"
                      @click="blockFn(scope.$index, scope.row)"
                      v-if="scope.row.status == 1"
                      >恢复</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="individual_user_pagination">
                <el-pagination
                  @size-change="listSizeChange"
                  @current-change="listCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="pageSizes"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                >
                </el-pagination>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-row v-else>
          <el-empty description="暂无数据"></el-empty>
        </el-row>
      </div>
    </el-row>
  </div>
</template>
<style src="../../assets/css/user/individualUser.css" scoped></style>

<script>
export default {
  name: "IndividualUser",
  components: {},
  data() {
    return {
      userListData: [], // 用户列表
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      searchKey: "", // 搜索关键字
      loadingKey: false,
      loadingText: "",
    };
  },
  created: function () {
    this.getListDataFn();
  },
  methods: {
    // 搜索
    searchFn: function () {
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 获取列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/users/index";
      let data = {
        type: 1,
        page: this.currentPage,
        perPage: this.pageSize,
        keywords: this.searchKey,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.userListData = response.user_info;
            that.total = response.total_count;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 拉黑/恢复
    blockFn: function (index, row) {
      let that = this;
      let status = row.status;
      let text = "";
      let url = "/admin/users/changeStatus";
      let data = {
        id: row.id,
      };
      if (status == 1) {
        text = "恢复";
        data.status = 0;
      } else {
        text = "拉黑";
        data.status = 1;
      }
      this.$confirm("确认" + text + "该用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        that.loadingText = "处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("操作成功");
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },
    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 完善信息格式化
    formatterIsRegisterFn: function (row) {
      let is_register = row.is_register;
      let text = "";
      if (is_register == 1) {
        text = "否";
      } else {
        text = "是";
      }
      return text;
    },
    // 证件类型格式化
    formatterIdentityTypeFn: function (row) {
      let identity_type = row.identity_type;
      let text = "";
      if (identity_type == 1) {
        text = "身份证";
      } else if (identity_type == 2) {
        text = "其他";
      }
      return text;
    },
    // 设置单元格字体颜色
    cellStyleFn: function ({ row, columnIndex }) {
      let style = "";
      if (columnIndex == 5) {
        if (row.is_register == 1) {
          style = "color: #F6705E";
        } else {
          style = "color: #8ECF5F";
        }
      }
      return style;
    },
  },
};
</script>
